package com.housesales.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import module.Customers;
import module.EmployeeCustomer;
import module.HousesCustomers;
import module.Phones;
import module.RequirementHouse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import utils.DateUtil;
import utils.PageUtil;
import bussinessService.basicbussinessService;
import bussinessService.customersBussinessService;
import bussinessService.phonesBussinessService;
import bussinessService.impl.CustomersBussinessServiceImpl;
import bussinessService.impl.PhonesBussinessServiceImpl;
import bussinessService.impl.RequirementHouseBussinessServiceImpl;
import domain.vo.CustomerQueryObject;
import domain.vo.CustomerVO;
import domain.vo.JSONObject;
import domain.vo.Message;
import domain.vo.PhoneVO;
import domain.vo.RequirementHousQueryObject;
import domain.vo.RequirementHousVO;
import exception.PrivateCustomerFullException;

/**
 * 客户
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/custome")
public class CustomeController {
	/**
	 * 系统配置客户跟踪过期期限 默认为10天
	 */
	private @Value("#{sysProperties.overdue}")
	long overdue;
	/**
	 * 每个员工最大私客数量
	 */
	private @Value("#{sysProperties.privateUserNumber}")
	int privateUserNumber;
	private Logger logger = Logger.getLogger(CustomeController.class);

	private basicbussinessService<RequirementHouse, RequirementHousVO> rhBSI = new RequirementHouseBussinessServiceImpl();

	@RequestMapping("/addCustome")
	public @ResponseBody
	Message saveCustome(RequirementHouse requirementHouse,
			HttpServletRequest request) {
		Message ms = new Message();

		String e_sn = request.getSession().getAttribute("e_sn").toString();
		String e_name = request.getSession().getAttribute("e_name").toString();
		String shop = request.getSession().getAttribute("shopId").toString();
		requirementHouse.setE_sn(e_sn);
		requirementHouse.setE_name(e_name);
		requirementHouse.setShopName(shop);
		RequirementHousQueryObject rhQueryObject = new RequirementHousQueryObject();
		rhQueryObject.setClazz(requirementHouse.getClazz());
		rhQueryObject.setProperty(requirementHouse.getProperty());
		rhQueryObject.setE_sn(e_sn);

		try {
			int len = rhBSI.countByQueryObject(rhQueryObject);
			if (len >= privateUserNumber) {
				throw new PrivateCustomerFullException("已经达到客户数量上限 "
						+ privateUserNumber + " 人");
			} else {
				rhBSI.merageObject(requirementHouse);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("客户新建失败");
		} catch (PrivateCustomerFullException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg(e.getMessage());
		}

		return ms;
	}

	@RequestMapping("/updateCustome")
	public @ResponseBody
	Message updateCustome(RequirementHouse requirementHouse,
			HttpServletRequest request) {
		Message ms = new Message();
		try {
			String shop = request.getSession().getAttribute("shopId")
					.toString();
			requirementHouse.setShopName(shop);
			rhBSI.merageObject(requirementHouse);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("客户新建失败");
		}
		return ms;
	}

	@RequestMapping("/viewList")
	public ModelAndView viewList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/admin/customer/customerList");
		return mv;
	}

	@RequestMapping("/viewCustomersJson/{type}.html")
	public @ResponseBody
	JSONObject<RequirementHousVO> viewCustomersJson(
			@RequestParam("page") int page, @RequestParam("rows") int rows,
			@RequestParam(value = "clazz", required = false) String clazz,
			@PathVariable("type") String type, HttpSession httpSession) {
		JSONObject<RequirementHousVO> jsonObject = new JSONObject<RequirementHousVO>();
		if (clazz != null) {
			try {
				RequirementHousQueryObject rhQueryObject = new RequirementHousQueryObject();
				rhQueryObject.setClazz(clazz);
				rhQueryObject.setProperty(type);
				if (Customers.PRIVATE.equals(type)) {
					rhQueryObject.setE_sn(httpSession.getAttribute("e_sn")
							.toString());
				}
				int len = rhBSI.countByQueryObject(rhQueryObject);
				PageUtil.Page(page, rows);
				rhQueryObject.setStart(PageUtil.start);
				rhQueryObject.setEnd(PageUtil.end);

				List<RequirementHousVO> vos = rhBSI
						.getObjectByQueryObject(rhQueryObject);
				jsonObject.setRows(vos);
				jsonObject.setTotal(len);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return jsonObject;
	}

}
